define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, $rootScope, T_login, GoPage, GoDialog, Auth, $stateParams, GoPush, GoWapAuth, $timeout) {
		$scope.input = new Object();
		$scope.input.isQQlogin = true;
		$scope.input.isWXlogin = true;
		$scope.input.username = '';
		$scope.input.password = '';
		$scope.input.rememberme = true;//默认为记住我

		$scope.onshow_register = function() {
			GoPage.go_states("t-register");
		};
		$scope.onshow_fingpsd = function() {
			GoPage.go_states("t-f-password");
		};
		$scope.loginBack = function() {
			GoPage.go_back(-1, 'login');
		};
		//画面加载
		$scope.$on('$ionicView.afterEnter', function(scope, status) {
			GoPage.init_top($scope, 44);
			//记录用户
			$scope.input.username = Auth.getremember();
			$scope.input.password ='';

			//检查微信登录
			if(window.cordova && window.cordova.plugins) {
				Wechat.isInstalled(function(installed) {
					if(!installed) {
						$scope.input.isWXlogin = false;
					} else {
						$scope.input.isWXlogin = true;
					}
				});
			} else {
				$scope.input.isWXlogin = true;
			}

			//检查QQ登录
			if(window.cordova && window.cordova.plugins) {
				QQSDK.checkClientInstalled(function() {
					$scope.input.isQQlogin = true;
				}, function() {
					$scope.input.isQQlogin = false;
				});
			} else {
				$scope.input.isQQlogin = true;
			}

			sessionStorage.removeItem("auth-headimgurl");
			sessionStorage.removeItem("auth-nickname");
		}, false);

		//账户登录
		$scope.onLogin = function() {
			if(!$scope.input.username) {
				GoDialog.alert("提示", "请输入账号！");
				return;
			}
			if($scope.input.password.length < 6) {
				GoDialog.alert("提示", "请至少输入6位密码！");
				return;
			}
			var sourceStr = "";
			if(ionic.Platform.isIOS()) {
				sourceStr = "ios";
			} else {
				sourceStr = "android";
			}
			var input = $scope.input;
			var param = {
				"uname": input.username,
				"password": input.password,
				"source": sourceStr
			}
			//登录操作
			T_login.get_login(param, function(res) {
				if(!res.success) {
					GoDialog.alert("提示", res.msg);
					return;
				}

				$timeout(function() {
					GoPush.regist();
				}, 100);
				var info = res.result;
				for(var k in info) {
					if(info[k] == null || info[k] == undefined) {
						info[k] = '';
					}
				}
				Auth.save_loginInfo(info); //保存用户信息在本地
				GoDialog.toast('登录成功');

				if($scope.input.rememberme == true) {
					Auth.saveremember($scope.input.username);
				} else {
					Auth.removeremember();
				}
				
				//用户返回
				GoPage.go_back();
//				window.location.href = "#/tab/home"
			}, function(e) {
				GoDialog.alert("提示", "请求失败，请再试一次");
			}, function() {

			});
		};

		//微信验证登录
		$scope.onWechat = function() {
			if(window.cordova && window.cordova.plugins) {
				//检查微信登录
				var scope = "snsapi_userinfo";
				var state = "_" + (+new Date());
				Wechat.auth(scope, state, function(response) {
					//接口登录
					var thirdid = response['code'];
					var thirdtype = "wx";
					//登录操作
					T_login.get_wechat({
						"id": thirdid,
						"type": thirdtype
					}, function(res) {
						if(!res.success) {
							GoDialog.alert("提示", res.msg);
							return;
						}
						if(res.result.islogin != undefined && !res.result.islogin) {
							//跳转用注册页面
							//							thirdid = res.result.unionid;
							//							GoDialog.confirm('确认', '您属于第一次登录，请完善注册信息', function() {
							//								GoPage.go_states("t-register", { "thirdid": thirdid, "thirdtype": thirdtype });
							//							});
							//授权注册//过度使用
							sessionStorage.setItem("auth-headimgurl", res.result.userinfo.headimgurl);
							sessionStorage.setItem("auth-nickname", res.result.userinfo.nickname);
							GoPage.go_states("t-login-auth", {
								'uuid': res.result.unionid,
								'type': 'wx'
							});
							return;
						}
						$timeout(function() {
							GoPush.regist();
						}, 100);

						var info = res.result;
						for(var k in info) {
							if(info[k] == null || info[k] == undefined) {
								info[k] = '';
							}
						}
						Auth.save_loginInfo(info);
						GoDialog.toast('登录成功');
						GoPage.go_back();
					}, function(e) {
						GoDialog.alert('提示', "请求失败，请再试一次");
					}, function() {

					});
				}, function(reason) {
					GoDialog.alert('提示', "失败: " + reason);
				});
			} else {
				//请求微信
				GoWapAuth.loginWX();
			}
		};

		//QQ验证登录
		$scope.onQQ = function() {
			if(window.cordova && window.cordova.plugins) {
				//QQ授权登录
				QQSDK.ssoLogin(function(args) {
					//方便下次继续调用QQ登录
					if(window.cordova && window.cordova.plugins) {
						QQSDK.logout(function() {}, function() {});
					}
					//接口登录
					var thirdid = args.access_token;
					var thirdtype = "qq";
					//登录操作
					T_login.get_qq({
						"id": thirdid,
						"type": thirdtype
					}, function(res) {
						if(!res.success) {
							GoDialog.alert("提示", res.msg);
							return;
						}
						if(res.result.islogin != undefined && !res.result.islogin) {
							//跳转用注册页面
							//							thirdid = res.result.unionid;
							//							GoDialog.confirm('确认', '您属于第一次登录，请完善注册信息', function() {
							//								GoPage.go_states("t-register", { "thirdid": thirdid, "thirdtype": thirdtype });
							//							});
							//授权注册//过度使用
							sessionStorage.setItem("auth-headimgurl", args.figureurl);
							sessionStorage.setItem("auth-nickname", args.nickname);
							GoPage.go_states("t-login-auth", {
								'uuid': res.result.unionid,
								'type': 'qq'
							});
							return;
						}

						$timeout(function() {
							GoPush.regist();
						}, 100);

						var info = res.result;
						for(var k in info) {
							if(info[k] == null || info[k] == undefined) {
								info[k] = '';
							}
						}
						Auth.save_loginInfo(info);
						GoDialog.toast('登录成功');
						GoPage.go_back();
					}, function(e) {
						GoDialog.alert('提示', "请求失败，请再试一次");
					}, function() {

					});
				}, function(failReason) {
					GoDialog.alert('提示', "失败: " + failReason);
				});

			} else {
				//请求QQ 获取Authorization Code
				GoWapAuth.loginQQ();
			}
		}

		//密码是否可见
		$scope.inputType = "password";
		$scope.showPassword = function() {
			$scope.inputType = $scope.inputType == "password" ? "text" : "password";
		}

	}

	controller.$inject = ['$scope', '$rootScope', 'T_login', 'GoPage', 'GoDialog', 'Auth', '$stateParams', 'GoPush', 'GoWapAuth', '$timeout'];

	return app.registerController("t_loginctrl", controller);
});